
#include "include/bobo.h"

#define i2 (i*i)


__attribute__ ((visibility ("default")))
__attribute__ ((section (".app")))
JNIEXPORT jstring getSign(JNIEnv *env, jobject thiz, jobject context)
{
    return GetSignature(env, thiz, context);
}

JNIEXPORT jstring _md5(JNIEnv *env, jobject clazz, jstring str)
{
    return raw_md5(env, clazz, str);
}

JNIEXPORT jstring my_sha1(JNIEnv *env,jobject clazz, jstring str)
{
    return raw_sha1(env, clazz, str);
}

jstring my_aesEcbEncryptWithKey(JNIEnv *env,jobject clazz,jstring key, jstring str)
{
    return raw_aesEcbEncrypt(env, clazz, key, str);
}

jstring my_aesEcbDecryptWithKey(JNIEnv *env,jobject clazz,jstring key, jstring str)
{
    return raw_aesEcbDecrypt(env, clazz, key, str);
}

jstring my_aesCbcEncrypt(JNIEnv *env,jobject clazz,jstring iv, jstring key, jstring str_)
{
    return raw_aesCbcEncrypt(env, clazz, iv, key, str_);
}

jstring my_aesCbcDecrypt(JNIEnv *env,jobject clazz,jstring iv, jstring key, jstring str_)
{
    return raw_aesCbcDecrypt(env, clazz, iv, key, str_);
}

__attribute__ ((visibility ("default")))
__attribute__ ((section (".app")))
JNIEXPORT jbyteArray crypt(JNIEnv *env,jobject clazz,jobject ctx,  jbyteArray ar)
{
    jsize length = (* env)->GetArrayLength(env,ar);
    jbyte *jb = (*env)->GetByteArrayElements(env,ar, 0);//得到一个指向ja的Java数组元素的C指针
    jbyte seed = (jbyte)0x99;
    for(int i =0 ; i < length; i++)
    {
        jb[i] = jb[i] ^ seed;
    }
    return jb;
}

__attribute__ ((visibility ("default")))
__attribute__ ((section (".app")))
JNIEXPORT jint WriteString2File(JNIEnv *env, jobject clazz, jstring path, jstring content)
{
    return WriteStringToFile(env, clazz, path, content);
}

__attribute__ ((visibility ("default")))
__attribute__ ((section (".app")))
JNIEXPORT jstring ReadString(JNIEnv *env, jobject clazz, jstring path)
{
    return ReadStringFromFile(env, clazz, path);
}

// Java和JNI函数的绑定表
static JNINativeMethod method_table[] = {
        {"getSign",                 "(Ljava/lang/Object;)Ljava/lang/String;",                     (void *) getSign},
        {"md5",                     "(Ljava/lang/String;)Ljava/lang/String;",                      (void *) _md5},
        {"sha1",                     "(Ljava/lang/String;)Ljava/lang/String;",                      (void *) my_sha1},
        {"aesEcbEncryptWithKey",   "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;", (void *) my_aesEcbEncryptWithKey},
        {"aesEcbDecryptWithKey",   "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;",  (void *) my_aesEcbDecryptWithKey},
        {"aesCbcEncrypt",           "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;", (void *) my_aesCbcEncrypt},
        {"aesCbcDecrypt",           "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;",  (void *) my_aesCbcDecrypt},
        {"crypt",                   "(Ljava/lang/Object;[B)[B",                                      (void *) crypt},
        {"writeString",             "(Ljava/lang/String;Ljava/lang/String;)I",                     (void *) WriteString2File},
        {"readString",              "(Ljava/lang/String;)Ljava/lang/String;" ,                     (void *) ReadString}
};

// 注册native方法到java中
static int registerNativeMethods(JNIEnv *env, const char *className,
                                 JNINativeMethod *gMethods, int numMethods)
{
    jclass clazz;
    clazz = (*env)->FindClass(env, className);
    if (clazz == NULL)
    {
        return JNI_FALSE;
    }
    if ((*env)->RegisterNatives(env, clazz, gMethods, numMethods) < 0)
    {
        return JNI_FALSE;
    }

    return JNI_TRUE;
}

int register_ndk_load(JNIEnv *env)
{
    // 注册调用方法
    return registerNativeMethods(env, JNIREG_CLASS, method_table, NELEM(method_table));
}

JNIEXPORT jint JNI_OnLoad(JavaVM *vm, void *reserved)
{
    JNIEnv *env = NULL;
    jint result = -1;

    if ((*vm)->GetEnv(vm, (void **) &env, JNI_VERSION_1_4) != JNI_OK)
    {
        return result;
    }

    register_ndk_load(env);

    // 返回jni的版本
    return JNI_VERSION_1_4;
}